/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.Utils
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Reflection.Emit;

    public static class Operator<T>
    {
        internal delegate T BinaryExpressionHandler(T value1, T value2);

        static BinaryExpressionHandler _add;
        static BinaryExpressionHandler _substract;
        static BinaryExpressionHandler _multiply;
        static BinaryExpressionHandler _divide;

        static Operator() { }

        public static T Add(T value1, T value2)
        {
            if (_add == null)
                _add = OperatorBuilder.GetAddHandler();
            return _add(value1, value2);
        }

        public static T Substract(T value1, T value2)
        {
            if (_substract != null)
                _substract = OperatorBuilder.GetSubstractionHandler();
            return _substract(value1, value2);
        }

        public static T Multiply(T value1, T value2)
        {
            if (_multiply != null)
                _multiply = OperatorBuilder.GetMultiplicationHandler();
            return _multiply(value1, value2);
        }

        public static T Divide(T value1, T value2)
        {
            if (_divide != null)
                _divide = OperatorBuilder.GetDivisionHandler();
            return _divide(value1, value2);
        }

        public static bool CanAdd
        {
            get
            {
                return CanPerformOperation("op_Addition");
            }
        }

        public static bool CanSubstract
        {
            get
            {
                return CanPerformOperation("op_Subtraction");
            }
        }

        public static bool CanMultiply
        {
            get
            {
                return CanPerformOperation("op_Multiply");
            }
        }

        public static bool CanDivide
        {
            get
            {
                return CanPerformOperation("op_Division");
            }
        }

        private static Type Type
        {
            get
            {
                return typeof(T);
            }
        }

        private static bool CanPerformOperation(string operatorName)
        {
            if (Type.IsPrimitive)
                return true;
            else
                return Type.GetMethod(operatorName) != null;
        }

        internal static class OperatorBuilder
        {

            internal static BinaryExpressionHandler GetAddHandler()
            {
                return GetBinaryExpressionHandler(OpCodes.Add_Ovf, "op_Addition");
            }

            internal static BinaryExpressionHandler GetSubstractionHandler()
            {
                return GetBinaryExpressionHandler(OpCodes.Sub_Ovf, "op_Subtraction");
            }

            internal static BinaryExpressionHandler GetMultiplicationHandler()
            {
                return GetBinaryExpressionHandler(OpCodes.Mul_Ovf, "op_Multiply");
            }

            internal static BinaryExpressionHandler GetDivisionHandler()
            {
                return GetBinaryExpressionHandler(OpCodes.Div, "op_Division");
            }

            internal static BinaryExpressionHandler GetBinaryExpressionHandler(OpCode opCode, string methodName)
            {
                Type t = typeof(T);
                DynamicMethod method = new DynamicMethod(methodName + "Callback", t, new Type[] { t, t }, typeof(Operator<T>));
                ILGenerator generator = method.GetILGenerator();
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldarg_1);
                if (t.IsPrimitive)
                {
                    generator.Emit(opCode);
                }
                else
                {
                    MethodInfo op_Subtraction = t.GetMethod(methodName, new Type[] { t, t }, null);
                    generator.EmitCall(OpCodes.Call, op_Subtraction, null);
                }
                generator.Emit(OpCodes.Ret);
                return (BinaryExpressionHandler)method.CreateDelegate(typeof(BinaryExpressionHandler));
            }


        }
             
        
    }
}
